﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Writer.Embedding.Predictors
{
    /// <summary>
    /// The Up predictor class is a predictor for the Deflate algorithm. It implements the
    /// 'PNG Up on all rows' scheme.
    /// </summary>
    [Predictor(PredictorCode.Up)]
    internal class UpPredictor : Predictor
    {
        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Retrieves the padding in data that is encoded using the current predictor.
        /// </summary>
        public override int Padding
        {
            get { return 1; }
        }
        #endregion



        // Base class overrides
        // ====================
        #region Base class overrides
        /// <summary>
        /// Decodes the specified buffer using the 'Up' scheme. In the 'Up' scheme, every point (x,y)
        /// in the two-dimensional space bounded by the buffer is altered to (x,y) + (x,y-1); that is,
        /// the value in the point just above it is added to each point in the matrix.
        /// </summary>
        /// <param name="buffer">The buffer</param>
        /// <param name="offset">The offset at which to decode.</param>
        /// <param name="width">The width of the matrix that the buffer represents.</param>
        public override void Decode(byte[] buffer, int offset, int width)
        {
            offset++; // ignore first value
            for (int x = 1; x < width; x++)
            {
                buffer[offset] = (byte)(buffer[offset] + buffer[offset - width]);
                // (x,y) := (x,y) + (x,y-1)

                offset++;
            }
        } // Decode()
        #endregion
    }
}
